package com.lwt.hospital.service.impl;

import com.lwt.hospital.dao.DemoDao;
import com.lwt.hospital.model.Appointment;
import com.lwt.hospital.model.Doctor;
import com.lwt.hospital.model.Question;
import com.lwt.hospital.model.User;
import com.lwt.hospital.service.UserService;
import com.lwt.hospital.utils.ProjectUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.print.Doc;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 丁兆顺
 * @date 2020/5/18 15:08
 * @desciption
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private DemoDao demoDao;

    @Override
    public String userRegister(User user, HttpSession httpSession) {
        if (demoDao.selectUserByCard(user.getCard()) == null && demoDao.selectUserByPhone(user.getPhone()) == null) {
            int result = demoDao.insertUser(user);
            if (result > 0) {
                user.setAccount(user.getCard());
                user.setIdentity(1);
                httpSession.setAttribute("userInfo", user);
                return "注册成功！";
            } else {
                return "注册失败！";
            }
        } else {
            return "该身份证或手机已注册，请登录！";
        }

    }

    @Override
    public boolean userIfLogin(HttpSession session) {
        if (session.getAttribute("userInfo") != null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 我这种肯定不合适
     * 可以参考两次查询或者按照长度判断
     *
     * @param account
     * @return
     */
    @Override
    public User getUserInfo(String account) {
        List<User> users = demoDao.selectAllUser();
        Map<String, User> cardUserMap = users.stream().collect(Collectors.toMap(User::getCard, User -> User));
        Map<String, User> phoneUserMap = users.stream().collect(Collectors.toMap(User::getPhone, User -> User));
        if (cardUserMap.containsKey(account)) {
            return cardUserMap.get(account);
        } else {
            return phoneUserMap.get(account);
        }
    }

    /**
     * 修改用户信息
     * @param user
     * @param session
     * @return
     */
    @Override
    public Integer modifyUserInfo(User user, HttpSession session) {
        User userInfo = (User) session.getAttribute("userInfo");
        User byPhone = demoDao.selectUserByPhone(user.getPhone());
        user.setId(userInfo.getId());
        user.setCard(userInfo.getCard());
        if (byPhone != null && byPhone.getId() != userInfo.getId()) {
            return -1;
        }else{
            int result = demoDao.modifyUserInfo(user);
            if(result > 0){
                User user1 = demoDao.selectUserInfo(user);
                user1.setIdentity(1);
                session.setAttribute("userInfo",user1);
                return 1;
            }else{
                return 0;
            }
        }
    }

    @Override
    public List<Appointment> selectAppointment(HttpSession session){
        User userInfo = (User) session.getAttribute("userInfo");
        List<Appointment> appointments = demoDao.selectAppointment(userInfo.getId(), "0");

        return null;
    }

    @Override
    public Integer addNewQuestion(Question question, HttpSession session){
        User userInfo =(User) session.getAttribute("userInfo");
        question.setUser(userInfo.getId());
        question.setStatus("0");
        return demoDao.addNewQuestion(question);
    }

    @Override
    public Integer addAppointment(Appointment appointment,HttpSession session){
        User userInfo =(User) session.getAttribute("userInfo");
        appointment.setUser(userInfo.getId());
        appointment.setStatus("0");
        return demoDao.addAppointment(appointment);
    }

    @Override
    public List<Appointment> getOldAppointment(HttpSession session){
        User userInfo =(User) session.getAttribute("userInfo");
        List<Appointment> oldAppointment = demoDao.getOldAppointment(userInfo.getId());
        oldAppointment.forEach(item->{
            item.setUserName(userInfo.getName());
            if(item.getDoctor() != 0){
                item.setDocName(demoDao.selectDoc(item.getDoctor()).getName());
            }
        });
        return oldAppointment;
    }

    @Override
    public List<Question> getOldQuestion(HttpSession session){
        User userInfo =(User) session.getAttribute("userInfo");
        List<Question> oldQuestion = demoDao.getOldQuestion(userInfo.getId());
        oldQuestion.forEach(item->{
            item.setUserName(userInfo.getName());
            if(item.getDoctor() != 0){
                item.setDocName(demoDao.selectDoc(item.getDoctor()).getName());
            }
        });
        return oldQuestion;
    }

    @Override
    public List<User> selectUsers(){
       return demoDao.selectAllUser();
    }

    @Override
    public int delUser(long id){
        return demoDao.delUser(id);
    }

}
